<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
 <head>
  <title>上传文件</title>
  <t:base type="jquery,easyui,tools,DatePicker"></t:base>
   <script src="plug-in/jquery-plugs/form/jquery.form.js"></script>
  <script type="text/javascript" charset="utf-8" src="plug-in/ueditor/ueditor.config.js"></script>
   <script type="text/javascript" charset="utf-8" src="plug-in/ueditor/ueditor.all.min.js"> </script> 
   <script type="text/javascript" charset="utf-8" src="plug-in/ueditor/lang/zh-cn/zh-cn.js"></script>
   <script type="text/javascript" src="plug-in/Validform/js/Validform_v5.3.1_min_zh-cn.js"></script>
   <script type="text/javascript" src="plug-in/Validform/js/Validform_Datatype_zh-cn.js"></script>
   <script type="text/javascript" src="plug-in/Validform/js/datatype_zh-cn.js"></script>
   <link rel="stylesheet" href="plug-in/Validform/css/style.css" type="text/css">
   <link rel="stylesheet" href="plug-in/Validform/css/tablefrom.css" type="text/css">
   
  	<style>
  		.prevImg{
  			float:left;
  			margin:2px;
  		}
  		.prevImg a{
  			display: block;
    		text-align: center;
    		cursor:pointer;
    		color:rgb(0,162,212)
  		}
  		.split{
		    background: white;
		    height: 30px;
		    line-height: 30px;
		    color: #8f0911;
		    font-weight: bold;
		    padding-left: 15px;
  		}
  	</style>
 </head>
 <body>
  <form id="form" title="上传视频" enctype="multipart/form-data" action="teachItemPreviewController.do?doUpdate" name="form" method="post">
  		<input type="hidden" id="btn_sub" class="btn_sub">
		<input type="hidden" name="id" value="${preview.id }"/>
		<input type="hidden" name="teachItemId" value="${preview.teachItem.id }"/>
		<table style="" cellpadding="0" cellspacing="1" class="formtable">
			<tbody>
				<tr>
					<td align="right">
						<label class="Validform_label">
							文件名:
						</label>
					</td>
					<td class="value">
					    <input id="name" name="name" type="text" 
					     	 style="width: 150px" class="inputxt" datatype="*" value="${preview.name }">
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">文件名</label>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							文件类型:
						</label>
					</td>
					<td class="value">
				     	 <input type="radio" name="fileType" value="0" checked="checked"/>视频   
				     	 <input type="radio" name="fileType" value="1"/>文档   
						<span class="Validform_checktip"></span>
						<label class="Validform_label" style="display: none;">文件类型</label>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							是否必学:
						</label>
					</td>
					<td class="value">
					     	<input type="radio" name="isMustLearn" value="0" checked="checked"/>否
				     	 	<input type="radio" name="isMustLearn" value="1"/>是
							<span class="Validform_checktip"></span>
							<label class="Validform_label" style="display: none;">设备品牌</label>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label class="Validform_label">
							文件:
						</label>
					</td> 
					<td class="value"> 
		         		
		         		<c:forEach items="${attList}" var="att">
		         			<div class="prevImg" id="prevImg${att.id}">
		         				<label>${att.origFileName }</label>
		         				<a onclick='deleteAttach(${att.id})'>删&nbsp;除</a>
		         			</div>  	 
		         		</c:forEach> 
		         		<input name="file" type="file"/>
						<button type="button" onclick="uploadFile('form')">上传</button>
		           </td> 
			  </tr>
			</tbody>
		</table>
		</form>
	<script>
		var form = $('#form').Validform({tiptype:"3"}); //初始化校验form控件
		
		$(document).ready(function () {
			$("body").find("[name='fileType'][value='${preview.fileType}']").attr("checked","checked");
			$("body").find("[name='isMustLearn'][value='${preview.isMustLearn}']").attr("checked","checked");
			 
			//判断是否是查看页面,不是查看页面，初始化编辑器
			if(location.href.indexOf("load=detail")==-1){
				UE.getEditor("detailEditor",{});//初始化editor控件
			}else{
				UE.getEditor("detailEditor",{
					readonly:true, 
					toolbars: []
				});//初始化editor控件
			}
			
			// ajaxSubmit
			$("#btn_sub").click(function () {
				var valid = form.check(false);
				if(!valid) return;
				
			     $("#form").ajaxSubmit({success: function (data) {
			    	data = $.parseJSON(data);
			        if(data.success){
			        	window.top.tip("保存成功！");
			        	frameElement.api.opener.reloadTable();
			        	frameElement.api.close();
			        }else{
			        	tip(data.msg);
			        }
				 }});
			});	
		});
		
		function deleteAttach(id){
			 $('#prevImg'+id).remove();
			 $('#form').append("<input type='hidden' name='deletedAttach'  value='"+id+"'/>");
		}
		
		function uploadFile(formId){
			if(typeof($('#'+ formId +' [name="file"]')[0].files[0]) == 'undefined'){
				tip('没有选择文件');
				return;
			}	
			var formData = new FormData();
			formData.append('file', $('#'+ formId +' [name="file"]')[0].files[0]);
			$.ajax({
			    url: 'cms-attach.do?upload',
			    type: 'POST',
			    cache: false,
			    data: formData,
			    dataType: 'json',
			    processData: false,
			    contentType: false,
			    success: function(data){
					if(data.success){
						$("#"+formId).append('<input type="hidden" name="attachId" value="'+ data.msg +'">');
						tip('上传成功');
						return;
					}else{
						tip(data.msg);
					}
				}
			});
		}
	</script>
</body>
 